• ▶︎SKTilemap

    Overview

    The SKTilemap class is a container for managing layers of tiles (sprites), vector objects & images. Tile data is stored in SKTileset tile sets.

    Usage

    Maps can be loaded with the class function SKTilemap.load(tmxFile:):

    if let tilemap = SKTilemap.load(tmxFile: "myfile.tmx") {
       scene.addChild(tilemap)
    }
    

    Properties

    Property Description
    mapSize Size of the map (in tiles).
    tileSize Map tile size (in pixels).
    renderSize Size of the map in pixels.
    orientation Map orientation (orthogonal, isometric, etc.)
    bounds Map bounding rect.
    tilesets Array of stored tileset instances.
    allowZoom Allow camera zooming.
    layers Array of child layers.
    See more

    Declaration

    Swift

    public class SKTilemap : SKEffectNode, SKTiledObject
  • ▶︎SKTileset

    Overview

    The tileset class manages a set of SKTilesetData objects, which store tile data including global id, texture and animation.

    Tile data is accessed via a local id, and tiles can be instantiated with the resulting SKTilesetData instance:

    if let data = tileset.getTileData(localID: 56) {
       let tile = SKTile(data: data)
    }
    

    Properties

    Property Description
    name Tileset name.
    tilemap Reference to parent tilemap.
    tileSize Tile size (in pixels).
    columns Number of columns.
    tilecount Tile count.
    firstGID First tile global id.
    lastGID Last tile global id.
    tileData Set of tile data structures.

    Instance Methods

    Method Description
    addTextures() Generate textures from a spritesheet image.
    addTilesetTile() Add & return new tile data object.
    See more

    Declaration

    Swift

    public class SKTileset : NSObject, SKTiledObject
  • Overview

    The SKTiledLayerObject is the generic base class for all layer types. This class doesn’t define any object or child types, but provides base behaviors for layered content, including:

    • coordinate transformations
    • validating coordinates
    • positioning and alignment

    Properties

    Property Description
    tilemap Parent tilemap.
    index Layer index. Matches the index of the layer in the source TMX file.
    size Layer size (in tiles).
    tileSize Layer tile size (in pixels).
    anchorPoint Layer anchor point, used to position layers.
    origin Layer origin point, used for placing tiles.

    Instance Methods

    Method Description
    pointForCoordinate(coord:offset:) Returns a point for a coordinate in the layer, with optional offset.
    coordinateForPoint(_:) Returns a tile coordinate for a given point in the layer.
    touchLocation(_:) Returns a converted touch location in map space.
    coordinateAtTouchLocation(_:) Returns the tile coordinate at a touch location.
    isValid(coord:) Returns true if the coordinate is valid.

    Usage

    Coordinate transformation functions return points in the current tilemap projection:

    node.position = tileLayer.pointForCoordinate(2, 1)
    

    Coordinate transformation functions translate points to map coordinates:

    coord = coordinateForPoint(touchPosition)
    

    Return the tile coordinate at a touch event (iOS):

    coord = imageLayer.coordinateAtTouchLocation(touchPosition)
    

    Return the tile coordinate at a mouse event (macOS):

    coord = groupLayer.coordinateAtMouseEvent(event: mouseClicked)
    
    See more

    Declaration

    Swift

    public class SKTiledLayerObject : SKEffectNode, SKTiledObject
  • ▶︎SKTileLayer

    Overview

    Subclass of SKTiledLayerObject, the tile layer is a container for an array of tiles (sprites). Tiles maintain a link to the map’s tileset via their SKTilesetData property.

    Properties

    Property Description
    tileCount Returns a count of valid tiles.

    Instance Methods

    Method Description
    getTiles() Returns an array of current tiles.
    getTiles(ofType:) Returns tiles of the given type.
    getTiles(globalID:) Returns all tiles matching a global id.
    getTilesWithProperty(:) Returns tiles matching the given property & value.
    animatedTiles() Returns all animated tiles.
    getTileData(globalID:) Returns all tiles matching a global id.
    tileAt(coord:) Returns a tile at the given coordinate, if one exists.

    Usage

    Accessing a tile at a given coordinate:

    let tile = tileLayer.tileAt(2, 6)!
    

    Query tiles of a certain type:

    let floorTiles = tileLayer.getTiles(ofType: "Floor")
    
    See more

    Declaration

    Swift

    public class SKTileLayer : SKTiledLayerObject
  • Overview

    The SKObjectGroup class is a container for vector object types. Most object properties can be set on the parent SKObjectGroup which is then applied to all child objects.

    Properties

    Property Description
    count Returns the number of objects in the layer.
    showObjects Toggle visibility for all of the objects in the layer.
    lineWidth Governs object line width for each object.
    debugDrawOptions Debugging display flags.

    Methods

    Method Description
    addObject Returns the number of objects in the layer.
    removeObject Toggle visibility for all of the objects in the layer.
    getObject(withID:) Returns an object with the given id, if it exists.

    Usage

    Adding a child object with optional color override:

    objectGroup.addObject(myObject, withColor: SKColor.red)
    

    Querying an object with a specific name:

    let doorObject = objectGroup.getObject(named: "Door")
    

    Returning objects of a certain type:

    let rockObjects = objectGroup.getObjects(ofType: "Rock")
    
    See more

    Declaration

    Swift

    public class SKObjectGroup : SKTiledLayerObject
  • ▶︎SKGroupLayer

    Overview

    Subclass of SKTiledLayerObject, the group layer is a container for managing groups of layers.

    Usage

    Query child layers:

    for child in group.layers {
       child.opacity = 0.5
    }
    

    Add layers to the group with:

    groupLayer.addLayer(playerLayer)
    

    Remove with:

    groupLayer.removeLayer(playerLayer)
    
    See more

    Declaration

    Swift

    public class SKGroupLayer : SKTiledLayerObject
  • ▶︎SKImageLayer

    Overview

    The SKImageLayer object is really nothing more than a sprite with positioning attributes.

    Properties

    Property Description
    image Layer image name.
    wrapX Wrap horizontally.
    wrapY Wrap vertically.

    Methods

    Method Description
    setLayerImage Set the layer’s image.
    setLayerTexture Set the layer’s texture.
    wrapY Wrap vertically.

    Usage

    Set the layer image with:

    imageLayer.setLayerImage("clouds-background")
    
    See more

    Declaration

    Swift

    public class SKImageLayer : SKTiledLayerObject
  • ▶︎SKTile

    Overview

    The SKTile class is a custom SpriteKit sprite node that references its data from a tileset.

    Tile data (including texture) is stored in SKTilesetData property.

    Properties

    Property Description
    tileSize tile size (in pixels)
    tileData tile data structure
    layer parent tile layer

    Instance Methods

    Method Description
    setupPhysics(shapeOf:isDynamic:) Setup physics for the tile.
    setupPhysics(rectSize:isDynamic:) Setup physics for the tile.
    setupPhysics(withSize:isDynamic:) Setup physics for the tile.
    runAnimation() Play tile animation (if animated).
    removeAnimation(restore:) Remove animation.
    runAnimationAsActions() Runs a SpriteKit action to animate tile tile (if animated).
    removeAnimationActions(restore:) Remove the animation for the current tile.
    See more

    Declaration

    Swift

    open class SKTile : SKSpriteNode
  • Overview

    The SKTilesetData object stores data for a single tileset tile, referencing the tile texture, animation frames (for animated tiles) as well as tile orientation.

    Also includes navigation properties for tile accessability, and graph node weight.

    Properties

    Property Description
    id Tile id (local)
    type Tiled type
    texture Tile texture
    tileOffset Tile offset
    See more

    Declaration

    Swift

    public class SKTilesetData : SKTiledObject
    extension SKTilesetData: Hashable
    extension SKTilesetData: CustomStringConvertible, CustomDebugStringConvertible
  • ▶︎SKTileObject

    Overview

    The SKTileObject class represents a Tiled vector object type (rectangle, ellipse, polygon & polyline). When the object is created, points can be added either with an array of CGPoint objects, or a string. In order to render the object, the SKTileObject.getVertices() method is called, which returns the points needed to draw the path.

    Properties

    Property Description
    id Tiled object id.
    size Object size.
    tileData Tile data (for tile objects).
    text Text string (for text objects). Setting this redraws the object.
    bounds Returns the bounding box of the shape.
    See more

    Declaration

    Swift

    open class SKTileObject : SKShapeNode, SKTiledObject
  • ▶︎SKTiledScene

    Overview

    Custom scene type for managing SKTilemap nodes.

    Conforms to the SKTiledSceneDelegate, SKTilemapDelegate & SKTilesetDataSource protocols.

    SKTiledScene Hierarchy

    Properties

    Property Description
    worldNode Root container node.
    tilemap Tile map object.
    cameraNode Custom scene camera.

    Instance Methods

    Method Description
    sceneDoubleTapped Called when the scene receives a double-tap event (iOS only).
    cameraPositionChanged Called when the camera positon changes.
    cameraZoomChanged Called when the camera zoom changes.
    cameraBoundsChanged Called when the camera bounds updated.
    sceneDoubleClicked Called when the scene is double-clicked (macOS only).
    mousePositionChanged Called when the mouse moves in the scene (macOS only).
    See more

    Declaration

    Swift

    open class SKTiledScene : SKScene, SKPhysicsContactDelegate, SKTiledSceneDelegate, SKTilemapDelegate, SKTilesetDataSource
    extension SKTiledScene: SKTiledSceneCameraDelegate
  • Overview

    Custom scene camera that responds to finger gestures and mouse events.

    The SKTiledSceneCamera is a custom camera meant to be used with a scene conforming to the SKTiledSceneDelegate protocol. The camera defines a position in the scene to render the scene from, with a reference to the SKTiledSceneDelegate.worldNode to interact with tile maps.

    Properties

    Property Description
    world World container node.
    delegates Array of delegates to notify about camera updates.
    zoom Camera zoom value.
    allowMovement Toggle to allow camera movement.
    minZoom Minimum zoom value.
    maxZoom Maximum zoom value.
    zoomClamping Clamping factor used to alleviate render artifacts like cracking.
    See more

    Declaration

    Swift

    public class SKTiledSceneCamera : SKCameraNode
  • Overview

    Custom GKGridGraphNode object that adds a weight parameter for use with Tiled scene properties. Can be used with normal GKGridGraphNode instances. The SKTiledGraphNode.weight property is used to affect the estimated cost to a connected node. (Increasing the weight makes it less likely to be travelled to, decreasing more likely).

    Usage

    // query a node in the graph and increase the weight property
    if let node = graph.node(atGridPosition: coord) as? SKTiledGraphNode {
       node.weight = 25.0
    }
    
    See more

    Declaration

    Swift

    public class SKTiledGraphNode : GKGridGraphNode, SKTiledObject
  • Overview

    A structure representing a single frame of animation. Time is stored in milliseconds.

    Properties

    Property Description
    id unique tile (local) id.
    duration frame duration.
    texture optional tile texture.
    See more

    Declaration

    Swift

    public class TileAnimationFrame : NSObject